package leetcode_1000;

public class VerifyingAnAlienDictionary_953 {
	public boolean isAlienSorted(String[] words, String order) {
		if(words.length<=1) return true;
        int hash[] = new int[26];
        char []chars = order.toCharArray();
        for(int i=0;i<chars.length;++i) {
        	hash[chars[i]-'a']=i;
        }
        for(int i=0;i<words.length-1;++i) {
        	int res = help(words[i],words[i+1],hash);
        	if(res==0)continue;
        	if(res==1) return false;
        }
        return true;
    }
	
	public int help(String str1,String str2,int hash[]) {
		int index =0 ;
		while(true) {
			if(index==str1.length()) {
				if(index == str2.length())
					return 0;
				return -1;
			}
			if(index == str2.length()) {
				return 1;
			}
			char c1 = str1.charAt(index);
			char c2 = str2.charAt(index);
			if(c1 == c2) {
				++index;
				continue;
			}
			else if(hash[c1-'a']<hash[c2-'a']) return -1;
			else return 1;
		}
	}
}
